﻿//
// GNS_PvpApplyTick.cpp
// MobileManageSys
//FuncTion  人人对战收税分成
// Created by mr.l on 16/1/16.
//
//
#include "GnsClientFrame.h"
#include "GNS_PvpApplyTick.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_Common.h"
USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()

//extern ISocketMgr * _GameSock;

GNS_PvpApplyTick::GNS_PvpApplyTick()
{
    _pDailyDate=nullptr;
    AllTitle=nullptr;
    AgentTitle=nullptr;
    PromoterTitle=nullptr;
    PlayerTitle=nullptr;
    allDataShow=nullptr;
    pTypeListVisw=nullptr;
    ShowText=nullptr;
    AllDate=nullptr;
    AllDateBg=nullptr;
    pCalendar=nullptr;
    TimeText=nullptr;
    GameName=nullptr;
    RecordlineCode=0;
    typeNum=0;
    SendType=0;
    GameType=nullptr;
    Mon=0;
    Year=0;
    Day=0;
}

GNS_PvpApplyTick::~GNS_PvpApplyTick()
{
    
}

GNS_PvpApplyTick* GNS_PvpApplyTick::createLayer()
{
    auto m_PopWndNode = GNS_PvpApplyTick::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(m_PopWndNode);
    return m_PopWndNode;
}

bool GNS_PvpApplyTick::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    /**
     *  @author MR.L, 16-02-16 11:02:50
     *
     *  获取当前时间
     */
    time_t tt;
    time(&tt);
    struct tm * time;
    time = localtime(&tt);
    
    Mon = (int)time->tm_mon+1;
    Year = time->tm_year+1900;
    Day  =(int)time->tm_mday;

	//
	_pDailyDate = GNS_DailyDate::createLayer();
	_pDailyDate->setVisible(false);
    return true;
}
void GNS_PvpApplyTick::SetNodeName(std::string CsbName)
{
    m_PopWndNode = CSLoader::createNode(CsbName);  //pGNS_....11....PvpApplyTick
    m_PopWndNode->setContentSize(winsize);
    ui::Helper::doLayout(m_PopWndNode);
    addChild(m_PopWndNode,11);
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(m_PopWndNode);
    auto pBackBtn = static_cast<Button*>(m_PopWndNode->getChildByName("PvPApplyTickBtn"));//返回按钮
    pBackBtn->setTag(1);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvpApplyTick::BtnCallBack, this));
    
    TimeText = static_cast<Text*>(m_PopWndNode->getChildByName("TimeText")); // 显示时间
    TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,Day));

    auto pTimeButton = static_cast<Button*>(m_PopWndNode->getChildByName("TimeBtn"));  // 时间上的Button
    pTimeButton->setTag(2);
    pTimeButton->addTouchEventListener(CC_CALLBACK_2(GNS_PvpApplyTick::BtnCallBack, this));
    
    auto pGameBtn = static_cast<Button *>(m_PopWndNode->getChildByName("GameBtn"));  //游戏 下拉 btn
    pGameBtn->setTag(3);
    pGameBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvpApplyTick::BtnCallBack, this));
    
    auto pTypebtn = static_cast<Button *>(m_PopWndNode->getChildByName("TypeBtn"));  // 类型 下拉  btn
    pTypebtn->setTag(4);
    pTypebtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvpApplyTick::BtnCallBack, this));
    
    
    auto pTimeSer = static_cast<Button*>(m_PopWndNode->getChildByName("Button_8")); // 时间框 btn
    pTimeSer->setTag(2);
    pTimeSer->addTouchEventListener(CC_CALLBACK_2(GNS_PvpApplyTick::BtnCallBack, this));
    
    auto typrbtnSrt = static_cast<Button*>(m_PopWndNode->getChildByName("Button_8_0"));  //类型框 btn
    typrbtnSrt->setTag(4);
    typrbtnSrt->addTouchEventListener(CC_CALLBACK_2(GNS_PvpApplyTick::BtnCallBack, this)); // 游戏框btn 
    
    auto gamebtn = static_cast<Button*>(m_PopWndNode->getChildByName("Button_8_1"));
    gamebtn->setTag(3);
    gamebtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvpApplyTick::BtnCallBack, this));
    
    GameName = static_cast<Text*>(m_PopWndNode->getChildByName("Text_3_0"));
//    if (GNS_PlayerMgr::getInstance()->getGameList()->size()!=0) {
//        GameName->setString(GNS_PlayerMgr::getInstance()->getGameList()->at(0).szGameName);
//    }
    
    
    ShowText=static_cast<Text*>(m_PopWndNode->getChildByName("ShowText"));
    
    AllTitle=static_cast<ImageView*>(m_PopWndNode->getChildByName("AllTilt"));
    AgentTitle=static_cast<ImageView*>(m_PopWndNode->getChildByName("AgentTilt"));
    AgentTitle->setVisible(false);
    PromoterTitle=static_cast<ImageView*>(m_PopWndNode->getChildByName("PromoterTilt"));
    PromoterTitle->setVisible(false);
    PlayerTitle=static_cast<ImageView*>(m_PopWndNode->getChildByName("PlayerTilt"));
    PlayerTitle->setVisible(false);
    TitleVec.push_back(AllTitle);
    TitleVec.push_back(AgentTitle);
    TitleVec.push_back(PromoterTitle);
    TitleVec.push_back(PlayerTitle);
    
    allDataShow=static_cast<ListView*>(m_PopWndNode->getChildByName("DateListView"));
    allDataShow->setVisible(false);
    pTypeListVisw = static_cast<ListView*>(m_PopWndNode->getChildByName("TypeListView"));
    pTypeListVisw->setVisible(false);
    
    GameType = static_cast<ListView*>(m_PopWndNode->getChildByName("GameType"));
    if (GameType!= nullptr)
        GameType->removeAllItems();
    
	int x = 0;
    for (auto it:(* GNS_PlayerMgr::getInstance()->getGameList())) {
       if (it.iGameType==2)
       {
            auto pbutton = Button::create("p0Btn.png");
			pbutton->setTitleText(SystemMgr::getInstance()->GbToUTF8(it.szGameName));
            pbutton->setTitleColor(Color3B::BLACK);
            pbutton->setTitleFontSize(20);
            pbutton->setTag(5);
            pbutton->addTouchEventListener(CC_CALLBACK_2(GNS_PvpApplyTick::BtnCallBack, this));
            GameType->addChild(pbutton);
			++x;
			if (x == 1)
				GameName->setString(SystemMgr::getInstance()->GbToUTF8(it.szGameName));
       }
    }
    GameType->setVisible(false);

    AllDate = static_cast<Text*>(m_PopWndNode->getChildByName("Text_8"));
    AllDate->setString("没有数据");
    AllDateBg = static_cast<ImageView*>(m_PopWndNode->getChildByName("Image_11"));
    
    
    auto TypeAllBtn = static_cast<Button*>(pTypeListVisw->getChildByName("Allbtn"));
    TypeAllBtn->setTag(5);
    TypeAllBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvpApplyTick::ListViewCallBack, this));
    
    auto TypeAgentBtn = static_cast<Button*>(pTypeListVisw->getChildByName("AgentBtn"));
    TypeAgentBtn->setTag(6);
    TypeAgentBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvpApplyTick::ListViewCallBack, this));
    
    auto TypePromoterBtn = static_cast<Button*>(pTypeListVisw->getChildByName("PromoterBtn"));
    TypePromoterBtn->setTag(7);
    TypePromoterBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvpApplyTick::ListViewCallBack, this));
    
    auto TypePlayerBtn = static_cast<Button*>(pTypeListVisw->getChildByName("PlayerBtn"));
    TypePlayerBtn->setTag(8); 
    TypePlayerBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvpApplyTick::ListViewCallBack, this));
    
    typeNum=GNS_PlayerMgr::getInstance()->getUserLevel();
    if (typeNum==0) {
        pTypeListVisw->removeChild(TypePromoterBtn);
        pTypeListVisw->removeChild(TypePlayerBtn);
    }else if (typeNum==1){
        pTypeListVisw->removeChild(TypeAgentBtn);
        pTypeListVisw->removeChild(TypePlayerBtn);
    }else if (typeNum==2){
        pTypeListVisw->removeChild(TypeAllBtn);
        pTypeListVisw->removeChild(TypeAgentBtn);
        pTypeListVisw->removeChild(TypePromoterBtn);
        ShowText->setString(TypePlayerBtn->getTitleText());
        AllDate->setVisible(false);
        AllDateBg->setVisible(false);
        allDataShow->setVisible(true);
        AllTitle->setVisible(false);
        PlayerTitle->setVisible(true);
        SendType=3;
    }
}

void GNS_PvpApplyTick::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){  // 选择游戏选项回调函数
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 1:
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.clear();
                    this->removeAllChildrenWithCleanup(true);
                     this->getParent()->removeChild(this);
                }
                //auto RevRepNode=GNS_PlayerMgr::getInstance()->_NodeHallVec.at(3);
                //RevRepNode->setVisible(true);

				GNS_NodeMgr::getInstance()->setMenuVisible("RevRep", true);
                break;
            }
            case 2:
            {
                 /*pCalendar =GNS_Calendar::createCalendar();
                 pCalendar->SetCalendBgImage("Calendarbg.png");
                std::vector<Button *> CalenderBtn;
                pCalendar->GetTimeButton()->swap(CalenderBtn);
                for (auto & it:CalenderBtn) {
                    it->setTag(100);
                    it->addTouchEventListener(CC_CALLBACK_2(GNS_PvpApplyTick::TimeButtonCallBack, this));
                }*/
				pCalendar = GNS_Calendar::createCalendar();
				pCalendar->setButtonsCallback(CC_CALLBACK_2(GNS_PvpApplyTick::TimeButtonCallBack, this));
                break;
            }
            case 3:
            {
                if (GameType->isVisible()==true) {
                    GameType->setVisible(false);
                }else{
                    GameType->setVisible(true);
                }
                break;
            }
            case 4:
            {
                if (pTypeListVisw->isVisible()==true) {
                    pTypeListVisw->setVisible(false);
                }else{
                    pTypeListVisw->setVisible(true);
                }
                break;
            }
            case 5:   //人人对战收税分成
            {
                GameName->setString(pButton->getTitleText());  //
                for (int i=0; i<GNS_PlayerMgr::getInstance()->getGameList()->size(); i++) {
					auto st1 = SystemMgr::getInstance()->utf8ToGb(pButton->getTitleText().c_str());
					auto st2 = GNS_PlayerMgr::getInstance()->getGameList()->at(i).szGameName;
					if (st1 == st2) {
                        GNS_PlayerMgr::getInstance()->getGameReveRes()->clear();
                        if (allDataShow != nullptr)
                            allDataShow->removeAllItems();
                        CMD_CS_GetManToManGameRevenue  pGetGameReven;
                        pGetGameReven._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                        pGetGameReven._uplineCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                        pGetGameReven._gameId=GNS_PlayerMgr::getInstance()->getGameList()->at(i).nGameId;
                        pGetGameReven._type=SendType;
                        std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
						std::string TimeDateTemp = SystemMgr::getInstance()->utf8ToGb(TimeDate.c_str());
						memcpy(pGetGameReven._selDate, TimeDateTemp.c_str(), 11);  //人人对战收税分成，选择游戏名称发消息
                        GNS_PlayerMgr::getInstance()->setGameId(pGetGameReven._gameId);
                        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMAN_GAME_REVENUE, (char*)&pGetGameReven, sizeof(pGetGameReven));
                        GameType->setVisible(false);
						GNS_PlayerMgr::getInstance()->setGameId(pGetGameReven._gameId);
                    }
                }
                
                break;
            }
            default:
                break;
        }
    }
}
void GNS_PvpApplyTick::ListViewCallBack(Ref*pSender,ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 5:
            {
                std::string btntext = pButton->getTitleText();
                ShowText->setString(btntext);
                pTypeListVisw->setVisible(false);
                TitleChooseType(AllTitle);
                SendType=0;
                break;
            }
            case 6:
            {
                std::string btntext = pButton->getTitleText();
                ShowText->setString(btntext);
                pTypeListVisw->setVisible(false);
                TitleChooseType(AgentTitle);
                SendType=1;
                break;
            }
            case 7:
            {
                std::string btntext = pButton->getTitleText();  //推广员
                ShowText->setString(btntext);
                pTypeListVisw->setVisible(false);
                TitleChooseType(PromoterTitle);
                SendType=2;
                break;
            }
            case 8:
            {
                std::string btntext = pButton->getTitleText();
                ShowText->setString(btntext);
                pTypeListVisw->setVisible(false);
                TitleChooseType(PlayerTitle);
                SendType=3;
                break;
            }
            default:
                break;
        }
        GNS_PlayerMgr::getInstance()->getGameReveRes()->clear();
        if (allDataShow != nullptr)
            allDataShow->removeAllItems();
        CMD_CS_GetManToManGameRevenue  pGetGameReven;
        pGetGameReven._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
        pGetGameReven._uplineCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
        pGetGameReven._gameId=GNS_PlayerMgr::getInstance()->getGameId();
        pGetGameReven._type=SendType;
        std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
		std::string TimeDateTemp = SystemMgr::getInstance()->utf8ToGb(TimeDate.c_str());
		memcpy(pGetGameReven._selDate, TimeDateTemp.c_str(), 11);    // 选择合计，，代理商 发消息
        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMAN_GAME_REVENUE, (char*)&pGetGameReven, sizeof(pGetGameReven));
    }
}
void GNS_PvpApplyTick::TitleChooseType(cocos2d::ui::ImageView *type)
{
    for (auto & it:TitleVec) {
        if (it==type) {
            it->setVisible(true);
        }else{
            it->setVisible(false);
        }
    }
    if (type==AllTitle) {
        AllDate->setVisible(true);
        AllDateBg->setVisible(true);
        allDataShow->setVisible(false);
    }else{
        AllDate->setVisible(false);
        AllDateBg->setVisible(false);
        allDataShow->setVisible(true);
    }
}
void GNS_PvpApplyTick::TimeButtonCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)  //选择时间回调函数
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        std::string BtnText = pButton->getTitleText();
        Mon = pCalendar->Mon;
        Year = pCalendar->year;
        Day = atoi(BtnText.c_str());
        TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,atoi(BtnText.c_str())));
        pCalendar->Show(false);
        
        if (allDataShow != nullptr)
            allDataShow->removeAllItems();
        GNS_PlayerMgr::getInstance()->getGameReveRes()->clear();
        CMD_CS_GetManToManGameRevenue  pGetGameReven;
        pGetGameReven._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
        pGetGameReven._uplineCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
        pGetGameReven._gameId=GNS_PlayerMgr::getInstance()->getGameId();
        pGetGameReven._type=SendType;
		std::string TimeDate = StringUtils::format("%d-%02d-%02d", Year, Mon, Day);
		std::string TimeDateTemp = SystemMgr::getInstance()->utf8ToGb(TimeDate.c_str());
		memcpy(pGetGameReven._selDate, TimeDateTemp.c_str(), 11);
        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMAN_GAME_REVENUE,(char*) &pGetGameReven, sizeof(pGetGameReven));
    }
}

void GNS_PvpApplyTick::AcceptSeverDate()  //根据接收到的服务器返回数据，对ListView
{
    if (SendType==0) {
        for (auto it:(*GNS_PlayerMgr::getInstance()->getGameReveRes())) {
            AllDate->setString(StringUtils::format("%lld",it._gameRevenue));
        }
    }else{
        if (allDataShow != nullptr)
            allDataShow->removeAllItems();
        int x = (int)GNS_PlayerMgr::getInstance()->getGameReveRes()->size();
        Size bgSize;
        int index=0;
        for (auto it:(*GNS_PlayerMgr::getInstance()->getGameReveRes())) {
            Button * itembg;  // 在listView 里，添加button 按钮
            itembg= Button::create("line between1.png");
            if (SendType==3) {
                itembg= Button::create("line2.png");
            }
            itembg->setTag(100+index);
            allDataShow->addChild(itembg);
            itembg->addTouchEventListener(CC_CALLBACK_2(GNS_PvpApplyTick::ListViewBtn, this));
            bgSize = itembg->getContentSize();
            
            auto Name = Text::create(StringUtils::format("%d",it._lowlineCode), GameFontName, 24);
            Name->setPosition(Vec2(bgSize.width*0.14, bgSize.height*0.4));
            Name->setColor(Color3B::BLACK);
            itembg->addChild(Name);
            
            auto lineRevenueRatio = Text::create(StringUtils::format("%lld",it._lowlineRevenueRatio), GameFontName, 24);
            lineRevenueRatio->setPosition(Vec2(bgSize.width*0.45, bgSize.height*0.4));
            lineRevenueRatio->setColor(Color3B::BLACK);
            itembg->addChild(lineRevenueRatio);
            
            auto agentRevenueRatio = Text::create(StringUtils::format("%lld",it._agentRevenueRatio), GameFontName, 24);
            agentRevenueRatio->setPosition(Vec2(bgSize.width*0.8, bgSize.height*0.4));
            agentRevenueRatio->setColor(Color3B::BLACK);
            itembg->addChild(agentRevenueRatio);
            index++;
        }
        if (bgSize.width!=0||bgSize.height!=0) {
            allDataShow->setInnerContainerSize(Size(bgSize.width, (bgSize.height+10)*x));
        }
    }
    
}

void GNS_PvpApplyTick::SeleLayer(int LayerNum)
{
    m_PopWndNode->setVisible(false);
	
	_pDailyDate->setVisible(true);
	_pDailyDate->removeAllChildren();

    if (LayerNum==11) {
            _pDailyDate->SetNodeName("PvpDialy.csb");
            _pDailyDate->SetProName(StringUtils::format("%d",RecordlineCode));
            _pDailyDate->AcceptSeverDate();
    }else if (LayerNum==12||LayerNum==21){
            _pDailyDate->AccepPlayerDate();
    }
}

void GNS_PvpApplyTick::ListViewBtn(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        if (SendType!=3) {
            auto pButton = (Button*)pSender;
            int BtnTag=pButton->getTag();
            int x =0;
            for (auto it:(*GNS_PlayerMgr::getInstance()->getGameReveRes())) {
                if (BtnTag-100==x) {
                    GNS_PlayerMgr::getInstance()->getGameProReveRes()->clear();
					GNS_PlayerMgr::getInstance()->getGamePlayReveRes()->clear();
                    if (allDataShow != nullptr)
                        allDataShow->removeAllItems();
                    CMD_CS_GetManToManGameRevenue  pGetGameReven;
                    pGetGameReven._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                    pGetGameReven._uplineCode=it._lowlineCode;
                    pGetGameReven._gameId=GNS_PlayerMgr::getInstance()->getGameId();
                    pGetGameReven._type=SendType+1;
                    std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
					std::string TimeDateTemp = SystemMgr::getInstance()->utf8ToGb(TimeDate.c_str());
					memcpy(pGetGameReven._selDate, TimeDateTemp.c_str(), 11);
                   //  人人对战收税分成， 第一层回调 
                    GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMAN_GAME_REVENUE,(char*) &pGetGameReven, sizeof(pGetGameReven));
                    GNS_PlayerMgr::getInstance()->RevRepMag=SendType;
                    RecordlineCode=it._lowlineCode;
                    break;
                }
                x++;
            }
        }
    }
}